package com.finger.gulimall.cart.controller;

import java.util.List;
import java.util.concurrent.ExecutionException;
import com.finger.common.utils.R;
import com.finger.gulimall.cart.service.CartService;
import com.finger.gulimall.cart.vo.Cart;
import com.finger.gulimall.cart.vo.CartItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class CartController {

	@Autowired
	private CartService cartService;

	/**
	 * 获取购物车里的所有商品
	 * @return Cart
	 */
	@RequestMapping("/cart.html")
	public String cartList(Model model) {
		// 获取购物车中的全部商品
		Cart cart = cartService.getCartWithItems();
		model.addAttribute("cart", cart);
		return "cartList";
	}

	/**
	 * 将商品添加到购物车
	 * @param skuId 商品id
	 * @param num   商品数量
	 * @return redirect:http://cart.gulimall.com/getItem/"+skuId
	 */
	@RequestMapping("/addToCart")
	public String success(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num, RedirectAttributes redirectAttributes) throws ExecutionException, InterruptedException {

		cartService.addToCart(skuId, num);
		redirectAttributes.addAttribute("skuId", skuId);
		return "redirect:http://cart.gulimall.com/addToCartSuccess.html";
	}

	/**
	 * 用于将商品添加到该购物车之后重定向到此请求作为缓冲
	 * @param skuId 商品id
	 * @return success
	 */
	@RequestMapping("/addToCartSuccess.html")
	public String getItem(@RequestParam("skuId") Long skuId, Model model) {
		// 根据商品id找到在该用户的购物车中的该项数据
		CartItem cartItem = cartService.getItem(skuId);
		model.addAttribute("item", cartItem);

		return "success";
	}

	/**
	 * 更新购物车中购物项的选中状态
	 * @param check 上皮内选中状态：选中为1，没选中为0
	 * @param skuId 需要修改商品的skuid
	 * @return 重定向到购物车
	 */
	@RequestMapping("checkChange.html")
	public String checkChange(@RequestParam(value = "check", required = false) Integer check, @RequestParam(value = "skuId", required = false) Long skuId) {
		// 更新购物车中购物项的选中状态
		cartService.updateCheckStatus(check, skuId);
		return "redirect:http://cart.gulimall.com/cart.html";
	}

	/**
	 * 更新购物车中购物项的数量
	 * @param skuId 需要修改商品的skuid
	 * @param num   需要修改商品的数量
	 * @return 重定向到购物车
	 */
	@RequestMapping("numChange.html")
	public String numChange(@RequestParam(value = "skuId", required = false) Long skuId, @RequestParam(value = "num", required = false) Integer num) {
		// 更新购物车中购物项的数量
		cartService.updateCountNum(skuId, num);

		return "redirect:http://cart.gulimall.com/cart.html";
	}

	/**
	 * 删除购物车中的商品
	 * @param skuId 待删除的商品id
	 * @return 重定向到购物车
	 */
	@RequestMapping("remove.html")
	public String removeItem(@RequestParam(value = "skuId", required = false) Long skuId) {
		// 移除购物车中的该商品
		cartService.removeItem(skuId);
		return "redirect:http://cart.gulimall.com/cart.html";
	}

	/**
	 * 供订单模块调用获取当前登录用户的全部购物项
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getItensForOrderConfrim")
	public R getItemsForOrderConfrim() {
		List<CartItem> cartItems = cartService.getItemsForOrderConfirm();
		return R.ok().setData(cartItems);
	}

}
